Back to Main Menu

Retrieving Documents

Assetic Document Retrieval Sample

The sample script below demonstrates how to retrieve a document from Assetic via the Assetic document API. For a more general overview of document retrieval, refer to this article on the Integrations section.

 

The Assetic Python SDK usually only returns the response body, and not headers. To support document download the Assetic Python SDK has an optional configuration to also return the "Content-Disposition" to allow the filename to be extracted.

 

  1. """
  2. Example script to download a document (Assetic.DocumentGet.py)
  3. For a given document id download the document
  4. """
  5. import assetic
  6. from assetic.rest import ApiException
  7. import re
  8. # Assetic SDK instance
  9. asseticsdk = assetic.AsseticSDK("c:/users/you/assetic.ini", None, "Error")
  10. filedir = "C:/temp/"
  11. docid = "c31b7d80-9a53-4d5c-a3b2-14a9f39077ec" # document to get
  12. # Assetic document API instance.
  13. docapi = assetic.DocumentApi()
  14. try:
  15. # get with http info because filename is in response header
  16. getfile = docapi.document_get_document_file_with_http_info(docid)
  17. except ApiException as e:
  18. asseticsdk.logger.error("Status {0}, Reason: {1} {2}"
  19. .format(e.status, e.reason, e.body))
  20. exit()
  21. if getfile is not None:
  22. # initially set filename to unknown
  23. fullfilename = filedir + "unknownfilename"
  24. # get document name from response. The response is a tuple.
  25. if "Content-Disposition" in getfile[2]:
  26. # extract document name from response.
  27. if ("attachment" in getfile[2]["Content-Disposition"]
  28. and "filename=" in getfile[2]["Content-Disposition"]):
  29. # overwrite unknown filename if found in response
  30. filename = getfile[2]["Content-Disposition"].split("filename=", 1)[1]
  31. if '"' in filename or "'" in filename:
  32. filename = filename[1:-1]
  33. # check and replace any invalid characters in the filename
  34. invalid_char_match = r'[^\w\-_\.]+'
  35. replace_char = '_'
  36. filename = re.sub(invalid_char_match, replace_char, filename)
  37. fullfilename = filedir + filename
  38. # get document data from response
  39. data = getfile[0]
  40. if type(data) is bytes:
  41. # cater for different binary data
  42. with open(fullfilename, "wb") as out_file:
  43. out_file.write(getfile[0])
  44. print("Created file: {0}".format(fullfilename))
  45. elif type(data) is str:
  46. # string data
  47. with open(fullfilename, "w", newline="", encoding="utf-8") as out_file:
  48. try:
  49. out_file.write(getfile[0])
  50. print("Created file: {0}".format(fullfilename))
  51. except UnicodeEncodeError as ex:
  52. print("Encoding error: ".format(str(ex)))
  53. else:
  54. print("File not created, data type unhandled: {0}".format(
  55. str(type(data))))

How it works

Initiate the Assetic Python SDK.

import assetic asseticsdk = assetic.AsseticSDK("c:/users/you/assetic.ini",None,"Error")

Define the directory to save the document to, and the GUID of the document.

filedir =  "C:/temp/" docid =  "c1ef6a41-aea4-e611-946c-06edd62964d7"

Create an instance of the document API.  

docapi = assetic.DocumentApi()

Since the response header contains the document name, ensure the header details are included by using the python method document_get_document_file_with_http_info() rather than the simpler method document_get_document_file()

The API request is wrapped in a try block to catch any exceptions which are raised by the API.  The error reason and response status can be written to the log as an error using "asseticsdk.logger"

try:  # get with http info because filename is in response header     getfile = docapi.document_get_document_file_with_http_info(docid)  except assetic.rest.ApiException  as e:     asseticsdk.logger.error("Status {0}, Reason: {1} {2}"  .format(e.status, e.reason, e.body))  exit();

The document content and header is returned as an array.

First check it is not None.  If the response returned a value, the header containing the file name of the document is the second value in the response array, which can be extracted.

Checks are done to find and replace any characters within the filename as a whole which cannot be used in a filename when saving the data locally. Invalid characters are replaced with appropriate substitute character, such as an underscore ( _ ).

if getfile is  not  None:  # initially set filename to unknown     fullfilename = filedir +  "unknownfilename"  # get document name from response. The response is a tuple.  if  "Content-Disposition"  in getfile[2]:  # extract document name from response.  if  ("attachment"  in getfile[2]["Content-Disposition"]  and  "filename="  in getfile[2]["Content-Disposition"]):  # overwrite unknown filename if found in response             filename = getfile[2]["Content-Disposition"].split("filename=",  1)[1]  if  '"'  in filename or  "'"  in filename:                 filename = filename[1:-1]  # check and replace any invalid characters in the filename             invalid_char_match = r'[^\w\-_\.]+'             replace_char =  '_'             filename = re.sub(invalid_char_match, replace_char, filename)             fullfilename = filedir + filename

The document file content is then retrieved, which is located in the first value of the response array.

  # get document data from response array     data = getfile[0]

Depending on the data type of the response the data is saved as either a binary, or a text file.

  if type(data)  is bytes:  # cater for different binary data  with open(fullfilename,  "wb")  as out_file:             out_file.write(getfile[0])  print("Created file: {0}".format(fullfilename))  elif type(data)  is str:  # string data  with open(fullfilename,  "w", newline="", encoding="utf-8")  as out_file:  try:                 out_file.write(getfile[0])  print("Created file: {0}".format(fullfilename))  except  UnicodeEncodeError  as ex:  print("Encoding error: ".format(str(ex)))  else:  print("File not created, data type unhandled: {0}".format(             str(type(data))))